<?
    final class AccountsController extends PrincipioLab_Controller_Action
    {
        const ACTION_VIEW = 0;
        const ACTION_EDIT = 1;
        const ACTION_ADD = 2;

        public function indexAction()
        {
            if ($this->is_post)
                $this->view->title = "Accounts";
        }


        private function validate()
        {
            if ($this->checkPostItemString("login", "Login"))
                $this->view->model_account->setLogin($this->post_data['login']);

            if ($this->checkPostItemString("password", "Password"))
                $this->view->model_account->setPassword($this->post_data['password']);

            if ($this->checkPostItemString("display_name", "Display name"))
                $this->view->model_account->setDisplayName($this->post_data['display_name']);

            if ($this->checkPostItemString("short_name", "Short name"))
                $this->view->model_account->setShortName($this->post_data['short_name']);

            if (!Principio_Common::$is_error)
                Principio_Common::checkValidity(Principio_Model_AccountMapper::checkUniqueLogin($this->view->model_account), "Account with login '".$this->view->model_account->getLogin()."' already exists in this company");

            return !Principio_Common::$is_error;
        }


        private function setActionViewData($action)
        {
            switch ($action)
            {
                case self::ACTION_EDIT;
                    $model_action_menu = new Principio_Model_ActionMenu();
                    $model_action_menu->setJsAjaxFunction("Accounts")->setItemID($this->view->model_account->getID())->
                        setMenu(Principio_Model_MenuItem::fromArray(['Edit' => "edit", 'Delete' => "delete"]))->
                        setAdditionalArgs(['company_id' => $this->view->model_account->getModelCompany()->getID()]);

                    $this->view->model_action_menu = $model_action_menu;

                    Principio_Common::setButtons(
                        "Apply",
                        "ajaxAccounts({action: 'edit', id: ".$this->view->model_account->getID().", company_id: ".$this->view->model_account->getModelCompany()->getID()."});",
                        "ajaxAccounts({id: ".$this->view->model_account->getID().", company_id: ".$this->view->model_account->getModelCompany()->getID().", action: 'edit', no_apply: true});"
                    );
                    break;

                case self::ACTION_ADD:
                    Principio_Common::setButtons(
                        "Add account",
                        "ajaxAccounts({id: -1, company_id: ".$this->view->model_account->getModelCompany()->getID().", action: 'add', use_inner: true});",
                        "addItem({menu: 'accounts', container: 'inner', company_id: ".$this->view->model_account->getModelCompany()->getID()."});"
                    );
                    break;
            };
        }


        public function viewAction()
        {
            return $this->editAction(self::ACTION_VIEW);
        }


        public function editAction($action = self::ACTION_EDIT)
        {
            if ($this->is_post)
            {
                if ($this->checkPostItemInt("id", "Account ID"))
                {
                    $this->view->action = ($action == self::ACTION_EDIT) ? "edit" : "view";
                    $this->view->model_account = Principio_Model_AccountMapper::fetch($this->post_data['id']);
                    $model_account_old = clone $this->view->model_account;

                    if ($this->is_apply)
                    {
                        if ($this->validate())
                        {
                            if ($this->view->model_account->save())
                                Principio_Common::reportMessage("Account was updated.");
                        };
                    };

                    $this->view->title = "View and Edit account [".(Principio_Common::$is_error ? $model_account_old->getLogin() : $this->view->model_account->getLogin())."]";
                    $this->setActionViewData(self::ACTION_EDIT);
                };
            };
        }


        public function addAction()
        {
            if (($this->is_post) && ($this->checkPostItemInt("company_id", "Company ID")))
            {
                $this->view->is_add = true;
                $this->view->action = "add";
                $model_company = Principio_Model_Company_AbstractMapper::fetch($this->post_data['company_id']);
                $this->view->title = "Adding new account in the company [".$model_company->getCaption()."]";
                $this->view->model_account = $model_company->createAccount();

                $this->setActionViewData(self::ACTION_ADD);

                if ($this->is_apply)
                {
                    if ($this->validate())
                    {
                        if ($this->view->model_account->add())
                        {
                            $this->view->action = "view";
                            $this->view->is_add = false;
                            Principio_Common::reportMessage("Account was added.");
                            $this->view->title = "View and Edit account '".$this->view->model_account->getLogin()."'";
                            $this->setActionViewData(self::ACTION_EDIT);
                        };
                    };
                };
            };
        }


        public function deleteAction()
        {
            if ($this->is_post)
            {
                if ($this->checkPostItemInt("id", "Account UD"))
                {
                    if (Principio_Model_AccountMapper::fetch($this->post_data['id'])->delete())
                        Principio_Common::reportMessage("Account was deleted.");
                };
            };
        }


        public function getlistAction()
        {
            if ($this->is_post)
            {
                if ($this->checkPostItemInt("company_id", "Company ID"))
                    $this->view->collection_account = Principio_Model_Company_AbstractMapper::fetch($this->post_data['company_id'])->getAccountCollection();
            };
        }
}